package com.itheima.demo05stream;


import org.junit.Test;

import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class Demo04StreamFunction {
    static List<String> list = new ArrayList<>();

    static {
        Collections.addAll(list, "迪丽热巴", "宋远桥", "苏星河", "老子", "庄子", "孙子");
    }


    @Test //forEach方法
    public void testForEach() {
        list.forEach(System.out::println);
    }

    @Test //count方法
    public void testCount() {
        System.out.println(list.stream().count());
    }

    @Test //filter方法
    public void testFilter() {
        list.stream().filter(str -> str.length() == 2).forEach(System.out::println);
    }

    @Test //limit方法 skip跳过前面元素
    public void testLimit() {
        list.stream().limit(2).forEach(System.out::println);
        list.stream().skip(1).forEach(System.out::println);
    }

    @Test //map方法
    public void testMap() {
        list.stream().map(String::length).forEach(System.out::println);
    }

    @Test //reduce 如果需要将所有数据归纳得到一个数据，可以使用 reduce 方法。方法签名
    public void testReduce() {
        Stream.of(4, 5, 3, 9).reduce((a, b) -> {
            System.out.println("a = " + a + ", b = " + b);
            return a + b;
        });

        int sum = Stream.of(4, 5, 3, 9).reduce(0, Integer::sum);
        int max = Stream.of(4, 5, 3, 9).reduce(0, (a, b) -> a > b ? a : b);
        System.out.println(sum);
        System.out.println(max);
    }


    @Test //实际操作
    public void testReduce2() {
        // 找出最大年龄
        int maxAge = Stream.of(
                new Person("刘德华", 58),
                new Person("张学友", 56),
                new Person("郭富城", 54),
                new Person("黎明", 52)).map(Person::getAge).reduce(0, (a, b) -> a > b ? a : b);
    }


    @Test //contact
    public void testContact() {
        // 合并集合
        Stream<String> streamA = Stream.of("张三");
        Stream<String> streamB = Stream.of("李四");

        Stream<String> stringStream = Stream.concat(streamA, streamB);
        stringStream.forEach(System.out::println);
    }

    //IntStream: 内部操作的是int类型的数据,就可以节省内存,减少自动装箱和拆箱
    @Test
    public void tesIntStream() {
        IntStream intStream = Stream.of(1, 2, 3, 4, 5).mapToInt(Integer::intValue);
        intStream.filter(n -> n > 3).forEach(System.out::println);
    }


    @Test
    public void testMapReduce() {
        // 统计 a 出现的次数
        //                       1    0     0    1    0    1
        Integer num = Stream.of("a", "c", "b", "a", "b", "a").map(n -> {
            if (n.equals("a")) {
                return 1;
            }
            return 0;
        }).reduce(0, Integer::sum);
        System.out.println(num);
    }

    @Test
    public void testMax_Min() {
        Optional<Integer> max = Stream.of(5, 3, 6, 1).max(Comparator.naturalOrder());
        System.out.println(max.get());

        Optional<Integer> min = Stream.of(5, 3, 6, 1).min(Comparator.naturalOrder());
        System.out.println(min.get());
    }

    @Test
    public void testFind() {
        Stream<Integer> stream = Stream.of(33, 11, 22, 5);
        Optional<Integer> streamAny = stream.findFirst();
        System.out.println(streamAny.orElse(0));
    }

    @Test
    public void testMatch() {
        Stream<Integer> stream = Stream.of(33, 11, 22, 5);
        boolean b = stream.allMatch(n -> n > 22);
        System.out.println(b);
    }

    @Test
    public void testDistinct(){
        Stream<Person> stream = Stream.of(
                new Person("貂蝉", 18),
                new Person("杨玉环", 20),
                new Person("杨玉环", 20),
                new Person("西施", 16),
                new Person("西施", 16),
                new Person("王昭君", 25)
        );

       // stream.distinct().forEach(System.out::println);
        // 所有的流对象只能使用一次
        stream.sorted(Comparator.comparing(Person::getAge).reversed()).distinct().forEach(System.out::println);
    }




}